//
//  LYViewController.m
//  LYPhotoBrowser
//
//  Created by LvYuan on 12/19/2016.
//  Copyright (c) 2016 LvYuan. All rights reserved.
//

#import "LYViewController.h"
#import "LYPhotoBrowser.h"
#import "UIImageView+WebCache.h"

@interface LYViewController ()<UITableViewDelegate,UITableViewDataSource,LYPhotoBrowserDelegate>
@property (nonatomic, strong) NSArray *srcStringArray;
@property (nonatomic, strong) NSMutableArray *photos;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation LYViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [[SDWebImageManager sharedManager].imageCache clearDisk];
    
    _srcStringArray = @[
                        @"http://ww2.sinaimg.cn/thumbnail/9ecab84ejw1emgd5nd6eaj20c80c8q4a.jpg",
                        @"http://ww2.sinaimg.cn/thumbnail/642beb18gw1ep3629gfm0g206o050b2a.gif",
                        @"http://ww4.sinaimg.cn/thumbnail/9e9cb0c9jw1ep7nlyu8waj20c80kptae.jpg",
                        @"http://ww3.sinaimg.cn/thumbnail/8e88b0c1gw1e9lpr1xydcj20gy0o9q6s.jpg",
                        @"http://ww2.sinaimg.cn/thumbnail/8e88b0c1gw1e9lpr2n1jjj20gy0o9tcc.jpg",
                        @"http://ww4.sinaimg.cn/thumbnail/8e88b0c1gw1e9lpr4nndfj20gy0o9q6i.jpg",
                        @"http://ww3.sinaimg.cn/thumbnail/8e88b0c1gw1e9lpr57tn9j20gy0obn0f.jpg",
                        @"http://ww2.sinaimg.cn/thumbnail/677febf5gw1erma104rhyj20k03dz16y.jpg",
                        @"http://ww4.sinaimg.cn/thumbnail/677febf5gw1erma1g5xd0j20k0esa7wj.jpg"
                        ];
    
    _photos = [NSMutableArray array];
    
    [_srcStringArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        LYPhoto * photo = [[LYPhoto alloc]init];
        photo.thumbPicUrl = obj;
        photo.largePicUrl = [obj stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"bmiddle"];
        [_photos addObject:photo];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _srcStringArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    UIImageView * photo = [cell viewWithTag:1];
    
    [photo sd_setImageWithURL:[NSURL URLWithString:_srcStringArray[indexPath.row]] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
    }];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    //启动图片浏览器
    LYPhotoBrowser *browser = [[LYPhotoBrowser alloc] init];
    browser.sourceView = [cell viewWithTag:1]; // 原图的父控件
    browser.imageCount = _srcStringArray.count; // 图片总数
    browser.currentImageIndex = indexPath.row;
    browser.delegate = self;
    [browser show];
}

#pragma mark - photobrowser代理方法

- (LYPhoto*)photoBrowser:(LYPhotoBrowser *)browser photoAtIndex:(NSInteger)index{
    if (index>_srcStringArray.count-1) {
        return nil;
    }
    LYPhoto * photo = [_photos objectAtIndex:index];
    return photo;
}

@end
